clear; clc;
load('data/recsys/yahoo-l.mat');

para.tau = 1.01;
[row, col, val] = find(data);

[m, n] = size(data);

clear user item data;

val = val - mean(val);
val = val/std(val);

traIdx = [];
tstIdx = [];
cnt = 0;
for i = 1:100
idx = randperm(floor(length(val)/100));

traIdx = cat(2, traIdx, idx(1:floor(length(idx)*0.5)) + cnt);
tstIdx = cat(2, tstIdx, idx(ceil(length(idx)*0.5): ceil(length(idx)*0.6)) + cnt);

cnt = cnt + length(idx);
end

clear cnt i idx;

para.test.row  = row(tstIdx);
para.test.col  = col(tstIdx);
para.test.data = val(tstIdx);
para.test.m = m;
para.test.n = n;

clear tstIdx;

row = row(traIdx);
col = col(traIdx);
val = val(traIdx);

traData = sparse(row, col, val, m, n);

clear traIdx m n val row col;

%% start testing
para.maxIter = 5000;
para.decay = 0.85;
para.tol = 20;

%% nuclear norm
lambda = 55;

% % ActiveSubspace
% method = 3;
% t = tic;
% [ ~, ~, ~, out{method,1} ] = mc_alt( traData', lambda, para );
% Time(method,1) = toc(t);
% RMSE(method,1) = out{method,1}.RMSE(end);
% 
% % FastMatComp
% t = tic;
% method = 4;
% para.maxR = 400;
% para.speedup = 1;
% para.regType = 1;
% para.exact = 0;
% % [~, ~, ~, out{method,1}] = AccSoftImpute( traData', lambda, para );
% [~, ~, ~, out{method,1}] = FastMatComp( traData', lambda, 1e+8, para );
% Time(method,1) = toc(t);
% RMSE(method,1) = out{method,1}.RMSE(end);
% 
% clear U S V t lambda;

% %% non-convex
% para.maxR = 15;
% para.tol = 400;
% for i = 3:5
%     
% para.decay = 0.925;   
% para.regType = i - 2;
% 
% switch(para.regType)
%     case 1
%         lambda = 150;
%         theta1 = lambda/2;
%     case 2
%         lambda = 45000;
%         theta1 = sqrt(lambda);
%     case 3
%         lambda = 150;
%         theta1 = 6;
% end
% 
% % FAST
% method = 2;
% 
% t = tic;
% para.speedup = 1;
% [~, ~, ~, out{method,i}] = FastMatComp( traData', lambda, theta1, para );
% Time(method,i) = toc(t);
% RMSE(method,i) = out{method,i}.RMSE(end);
% 
% clear U S V t method estRank;
% 
% end
% 
% %% Fixed Rank
% estRank = 10;
% method = 1;
% t = tic;
% [~, ~, ~, out{method,2} ] = SoftImputeALS( traData', 1e-6, estRank, para );
% Time(method,2) = toc(t);
% RMSE(method,2) = out{method,2}.RMSE(end);
% 
% estRank = 200;
% method = 2;
% t = tic;
% [~, ~, ~, out{method,2} ] = EOR1MP( traData', estRank, para );
% Time(method,2) = toc(t);
% RMSE(method,2) = out{method,2}.RMSE(end);
% 
% 
% clear U S V method t estRank;
% clear i col row theta1 traData traIdx tstIdx user val data item para lambda;
% 
% save('yahoo-lar');